﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Assets.Editor.BehaviorEditor.View
{
    public enum ConnectionPointType { In, Out }
    public class ConnectionPoint
    {
        public Rect rect;

        public ConnectionPointType type;

        public Node node;

        public GUIStyle style;

        public List<Connection> connections;

        public Action<ConnectionPoint> OnClickConnectionPoint;

        public ConnectionPoint(
            Node node
            , ConnectionPointType type
            , Action<ConnectionPoint> OnClickConnectionPoint)
        {
            this.node = node;
            this.type = type;
            this.OnClickConnectionPoint = OnClickConnectionPoint;
            rect = new Rect(0, 0, 10f, 20f);

            if (type == ConnectionPointType.In)
            {
                style = (GUIStyle)"ButtonLeft";
            }
            else if (type == ConnectionPointType.Out)
            {
                style = (GUIStyle)"ButtonRight";
            }

            connections = new List<Connection>();
        }

        public void Draw()
        {
            rect.y = node.rect.y + (node.rect.height * 0.5f) - rect.height * 0.5f;

            switch (type)
            {
                case ConnectionPointType.In:
                    rect.x = node.rect.x - rect.width;
                    break;

                case ConnectionPointType.Out:
                    rect.x = node.rect.x + node.rect.width;
                    break;
            }

            if (GUI.Button(rect, "", style))
            {
                if (OnClickConnectionPoint != null)
                {
                    OnClickConnectionPoint(this);
                }
            }
        }
    }
}
